package com.itheima.udp4;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/*
接受不断的接收数据
 */
public class JieshouClient {
    public static void main(String[] args) throws IOException {
        //创建接受对象
        DatagramSocket ds = new DatagramSocket(8888);

        //创建数据对象
        byte[] bytes = new byte[1024 * 64];
        while (true) {
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
            //接受
            ds.receive(dp);

            //打印数据
            String s = new String(bytes, 0, dp.getLength());
            InetAddress address = dp.getAddress();
            String hostName = address.getHostName();
            String ip = address.getHostAddress();
            System.out.println("来自"+ip+"的"+hostName+"发来了"+s);

        }


        //释放资源
       // ds.close();


    }

}
